import { CabinetData, EnvironmentalSensor, PowerDevice, HVACDevice } from '../types';

/**
 * 生成机柜数据
 */
export const generateCabinets = (): CabinetData[] => {
  const generatedCabinets: CabinetData[] = [];
  const layout = [
    // 第一排机柜
    { row: 0, col: 0, x: -8, z: -8 },
    { row: 0, col: 1, x: -6, z: -8 },
    { row: 0, col: 2, x: -4, z: -8 },
    { row: 0, col: 3, x: -2, z: -8 },
    { row: 0, col: 4, x: 0, z: -8 },
    { row: 0, col: 5, x: 2, z: -8 },
    { row: 0, col: 6, x: 4, z: -8 },
    { row: 0, col: 7, x: 6, z: -8 },
    { row: 0, col: 8, x: 8, z: -8 },
    
    // 第二排机柜
    { row: 1, col: 0, x: -8, z: -5 },
    { row: 1, col: 1, x: -6, z: -5 },
    { row: 1, col: 2, x: -4, z: -5 },
    { row: 1, col: 3, x: -2, z: -5 },
    { row: 1, col: 4, x: 0, z: -5 },
    { row: 1, col: 5, x: 2, z: -5 },
    { row: 1, col: 6, x: 4, z: -5 },
    { row: 1, col: 7, x: 6, z: -5 },
    { row: 1, col: 8, x: 8, z: -5 },
    
    // 第三排机柜（反向）
    { row: 2, col: 0, x: -8, z: 2 },
    { row: 2, col: 1, x: -6, z: 2 },
    { row: 2, col: 2, x: -4, z: 2 },
    { row: 2, col: 3, x: -2, z: 2 },
    { row: 2, col: 4, x: 0, z: 2 },
    { row: 2, col: 5, x: 2, z: 2 },
    { row: 2, col: 6, x: 4, z: 2 },
    { row: 2, col: 7, x: 6, z: 2 },
    { row: 2, col: 8, x: 8, z: 2 },
    
    // 第四排机柜（反向）
    { row: 3, col: 0, x: -8, z: 5 },
    { row: 3, col: 1, x: -6, z: 5 },
    { row: 3, col: 2, x: -4, z: 5 },
    { row: 3, col: 3, x: -2, z: 5 },
    { row: 3, col: 4, x: 0, z: 5 },
    { row: 3, col: 5, x: 2, z: 5 },
    { row: 3, col: 6, x: 4, z: 5 },
    { row: 3, col: 7, x: 6, z: 5 },
    { row: 3, col: 8, x: 8, z: 5 },
    
    // 第五排机柜（垂直排列）
    { row: 4, col: 0, x: -10, z: -10 },
    { row: 4, col: 1, x: -10, z: -7 },
    { row: 4, col: 2, x: -10, z: -4 },
    { row: 4, col: 3, x: -10, z: -1 },
    
    // 第六排机柜（垂直排列）
    { row: 5, col: 0, x: 10, z: -10 },
    { row: 5, col: 1, x: 10, z: -7 },
    { row: 5, col: 2, x: 10, z: -4 },
    { row: 5, col: 3, x: 10, z: -1 }
  ];
  
  layout.forEach((pos, index) => {
    generatedCabinets.push({
      id: `cabinet-${index + 1}`,
      name: `服务器机柜 ${index + 1}`,
      model: 'standard',
      manufacturer: '华为',
      location: `机房A-区域${String.fromCharCode(65 + pos.row)}`,
      totalU: 42,
      usedU: Math.floor(Math.random() * 20) + 10,
      temperature: Math.floor(Math.random() * 10) + 20,
      humidity: Math.floor(Math.random() * 20) + 40,
      powerUsage: Math.floor(Math.random() * 500) + 200,
      status: Math.random() > 0.1 ? 'normal' : (Math.random() > 0.5 ? 'warning' : 'critical'),
      // 修改机柜的y坐标，使其贴合地板
      position: { x: pos.x, y: 0.75, z: pos.z },
      dimensions: {
        // 机柜尺寸 - 现实合理比例
        width: 0.8,  // 标准机柜宽度约800mm
        depth: 1.0,  // 标准机柜深度约1000mm
        height: 2.0  // 标准42U机柜高度约2000mm
      },
      devices: [],
      floorNumber: 1,
      roomId: 'room-1'
    });
  });
  
  return generatedCabinets;
};

// 传感器类型配置
const sensorTypeConfig = {
  temperature: {
    name: '温度',
    generateValue: () => Math.floor(Math.random() * 8) + 22,
    unit: '°C',
    isWarning: (value: number) => value > 80
  },
  humidity: {
    name: '湿度',
    generateValue: () => Math.floor(Math.random() * 15) + 45,
    unit: '%',
    isWarning: (value: number) => value > 80
  },
  smoke: {
    name: '烟雾',
    generateValue: () => Math.random() > 0.9 ? 1 : 0,
    unit: 'ppm',
    isWarning: (value: number) => value > 0
  },
  waterLeak: {
    name: '水浸',
    generateValue: () => Math.random() > 0.95 ? 1 : 0,
    unit: '状态',
    isWarning: (value: number) => value > 0
  },
  airFlow: {
    name: '气流',
    generateValue: () => Math.floor(Math.random() * 500) + 500,
    unit: 'm³/h',
    isWarning: (value: number) => value > 80
  }
};

/**
 * 生成环境传感器数据
 */
export const generateEnvironmentalSensors = (): EnvironmentalSensor[] => {
  const sensors: EnvironmentalSensor[] = [];
  
  // 传感器位置分布在房间各个区域
  const positions = [
    // 顶部传感器
    { x: -8, y: 4, z: -8 },
    { x: -4, y: 4, z: -8 },
    { x: 0, y: 4, z: -8 },
    { x: 4, y: 4, z: -8 },
    { x: 8, y: 4, z: -8 },
    
    { x: -8, y: 4, z: -5 },
    { x: -4, y: 4, z: -5 },
    { x: 0, y: 4, z: -5 },
    { x: 4, y: 4, z: -5 },
    { x: 8, y: 4, z: -5 },
    
    { x: -8, y: 4, z: 2 },
    { x: -4, y: 4, z: 2 },
    { x: 0, y: 4, z: 2 },
    { x: 4, y: 4, z: 2 },
    { x: 8, y: 4, z: 2 },
    
    { x: -8, y: 4, z: 5 },
    { x: -4, y: 4, z: 5 },
    { x: 0, y: 4, z: 5 },
    { x: 4, y: 4, z: 5 },
    { x: 8, y: 4, z: 5 }
  ];
  
  // 烟雾传感器的特定位置（调整为稍低的位置，更贴近机柜高度）
  const smokePositions = [
    { x: -7, y: 3, z: -7 },
    { x: 7, y: 3, z: -7 },
    { x: -7, y: 3, z: 3 },
    { x: 7, y: 3, z: 3 },
    { x: 0, y: 3, z: 0 }
  ];
  
  // 确保各种类型的传感器均匀分布
  const typeOrder = ['temperature', 'humidity', 'smoke', 'waterLeak', 'airFlow'] as const;
  
  let smokeIndex = 0;
  
  positions.forEach((pos, index) => {
    const typeIndex = index % typeOrder.length;
    const type = typeOrder[typeIndex];
    const config = sensorTypeConfig[type];
    
    // 如果是烟雾传感器，使用特定位置
    const sensorPosition = type === 'smoke' && smokeIndex < smokePositions.length 
      ? smokePositions[smokeIndex++]
      : pos;
    
    const value = config.generateValue();
    const status = config.isWarning(value) ? 'warning' : 'normal';
    
    sensors.push({
      id: `sensor-${index + 1}`,
      name: `${config.name}传感器 ${index + 1}`,
      type,
      location: `机房A-区域${String.fromCharCode(65 + Math.floor(index / 6))}`,
      value,
      unit: config.unit,
      status,
      position: sensorPosition,
      updateInterval: 60000,
      lastUpdated: new Date().toISOString()
    });
  });
  
  return sensors;
};

/**
 * 生成电力设备数据（包括UPS）
 */
export const generatePowerDevices = (): PowerDevice[] => {
  const devices: PowerDevice[] = [];
  const powerTypes = ['UPS', 'PDU', 'Transformer', 'Generator'] as const;
  
  // 电力设备的位置布局（通常在机房角落）
  const positions = [
    { x: -8, y: 1, z: -12 },  // 左上
    { x: 8, y: 1, z: -12 },   // 右上
    { x: -8, y: 1, z: 12 },   // 左下
    { x: 8, y: 1, z: 12 },    // 右下
    { x: -10, y: 1, z: 0 },   // 左侧中部
    { x: 10, y: 1, z: 0 }     // 右侧中部
  ];
  
  positions.forEach((pos, index) => {
    // 确保UPS设备至少有两个
    let type: typeof powerTypes[number];
    if (index < 2) {
      type = 'UPS';
    } else {
      type = powerTypes[index % powerTypes.length];
    }
    
    const status = Math.random() > 0.1 ? 'online' : (Math.random() > 0.5 ? 'warning' : 'error');
    
    devices.push({
      id: `power-${index + 1}`,
      name: `${type === 'UPS' ? '不间断电源' : type === 'PDU' ? '配电单元' : type === 'Transformer' ? '变压器' : '发电机'} ${index + 1}`,
      type,
      location: `机房A-电力区${index + 1}`,
      status,
      inputVoltage: Math.floor(Math.random() * 50) + 200,
      outputVoltage: Math.floor(Math.random() * 10) + 220,
      current: Math.floor(Math.random() * 20) + 5,
      powerRating: Math.floor(Math.random() * 1000) + 500,
      efficiency: Math.floor(Math.random() * 20) + 80,
      batteryLevel: type === 'UPS' ? Math.floor(Math.random() * 100) : undefined,
      position: pos
    });
  });
  
  return devices;
};

/**
 * 生成HVAC设备数据（空调等）
 */
export const generateHVACDevices = (): HVACDevice[] => {
  const devices: HVACDevice[] = [];
  const hvacTypes = ['AC', 'Heater', 'Fan', 'Ventilation'] as const;
  
  // HVAC设备的位置布局（通常在墙壁附近）
  const positions = [
    { x: -9, y: 1, z: 0 },    // 左侧
    { x: 9, y: 1, z: 0 },     // 右侧
    { x: 0, y: 1, z: -14 },   // 后侧
    { x: 0, y: 1, z: 14 },    // 前侧
    { x: -7, y: 1, z: -7 },   // 左上
    { x: 7, y: 1, z: -7 },    // 右上
    { x: -7, y: 1, z: 7 },    // 左下
    { x: 7, y: 1, z: 7 }      // 右下
  ];
  
  positions.forEach((pos, index) => {
    // 确保空调设备至少有两个
    let type: typeof hvacTypes[number];
    if (index < 2) {
      type = 'AC';
    } else {
      type = hvacTypes[index % hvacTypes.length];
    }
    
    const status = Math.random() > 0.1 ? 'online' : (Math.random() > 0.5 ? 'warning' : 'error');
    
    // 根据设备类型生成适当的温度设置
    const temperatureSetting = type === 'AC' || type === 'Heater' ? 
                              (type === 'AC' ? Math.floor(Math.random() * 10) + 16 : 
                                              Math.floor(Math.random() * 10) + 25) : 22;
    
    // 生成风速数据
    const fanSpeed = Math.floor(Math.random() * 3) + 1;
    
    // 生成能耗数据
    const energyConsumption = Math.floor(Math.random() * 500) + 100;
    
    devices.push({
      id: `hvac-${index + 1}`,
      name: `${type === 'AC' ? '空调' : type === 'Heater' ? '加热器' : type === 'Fan' ? '风扇' : '通风设备'} ${index + 1}`,
      type,
      location: `机房A-${type === 'AC' ? '空调区' : type === 'Heater' ? '加热区' : type === 'Fan' ? '风扇区' : '通风区'}${index + 1}`,
      status,
      temperatureSetting,
      fanSpeed,
      energyConsumption,
      position: pos
    });
  });
  
  return devices;
};